Contents
  1. 1. 一、安装配置mongodb
  2. 2. Step 1: 设置系统环境及确保缺省端口27107可用
  3. 3. Step 2: 下载安装文件
  4. 4. Step 3: 解压下载的文件
  5. 5. Step 4: 复制解压文件到运行目录
  6. 6. Step 5: 设置环境变量
  7. 7. Step 6: 创建数据目录
  8. 8. 二、启动及验证mongodb
  9. 9. 三、mongodb相关工具
    1. 9.0.0.1. mongod –help|more
    2. 9.0.0.2. mongod –version

MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
是一个介于关系数据 库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储 比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大 部分功能,而且还支持对数据建立索引。

MongoDB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。MongoDB很好的实现了面向对象的 思想(OO思想),在MongoDB中 每一条记录都是一个Document对象。MongoDB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的 实现CRUD操作。本文介绍了如何快速安装mongodb供大家参考。

一、安装配置mongodb

useradd mongodb

Step 1: 设置系统环境及确保缺省端口27107可用


当前环境

cat /etc/issue

vi /etc/selinux/config

SELINUX=disabled

Step 2: 下载安装文件


下载地址:

https://www.mongodb.org/downloads.

或者直接在命令提示符下使用curl命令下载

curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.1.tgz

Step 3: 解压下载的文件


pwd

/home/mongo

tar -xvf mongodb-linux-x86_64-rhel62-3.2.1.gz

注,本文直接从网站下载,所以文件为.gz

[mongo@mysql ~]$ tar -xvf mongodb-linux-x86_64-3.2.1.tgz
mongodb-linux-x86_64-3.2.1/README
tar: mongodb-linux-x86_64-3.2.1/README: time stamp 2016-01-12 02:37:17 is 48732.469243332 s in the future
mongodb-linux-x86_64-3.2.1/THIRD-PARTY-NOTICES
tar: mongodb-linux-x86_64-3.2.1/THIRD-PARTY-NOTICES: time stamp 2016-01-12 02:37:17 is 48732.468806195 s in the future
mongodb-linux-x86_64-3.2.1/MPL-2
tar: mongodb-linux-x86_64-3.2.1/MPL-2: time stamp 2016-01-12 02:37:17 is 48732.468729339 s in the future
mongodb-linux-x86_64-3.2.1/GNU-AGPL-3.0
tar: mongodb-linux-x86_64-3.2.1/GNU-AGPL-3.0: time stamp 2016-01-12 02:37:17 is 48732.468416571 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongodump
tar: mongodb-linux-x86_64-3.2.1/bin/mongodump: time stamp 2016-01-12 02:37:30 is 48745.395380045 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongorestore
tar: mongodb-linux-x86_64-3.2.1/bin/mongorestore: time stamp 2016-01-12 02:37:28 is 48743.308178108 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongoexport
tar: mongodb-linux-x86_64-3.2.1/bin/mongoexport: time stamp 2016-01-12 02:37:25 is 48740.256976344 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongoimport
tar: mongodb-linux-x86_64-3.2.1/bin/mongoimport: time stamp 2016-01-12 02:37:26 is 48741.205864231 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongostat
tar: mongodb-linux-x86_64-3.2.1/bin/mongostat: time stamp 2016-01-12 02:37:23 is 48738.158042438 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongotop
tar: mongodb-linux-x86_64-3.2.1/bin/mongotop: time stamp 2016-01-12 02:37:31 is 48746.110027909 s in the future
mongodb-linux-x86_64-3.2.1/bin/bsondump
tar: mongodb-linux-x86_64-3.2.1/bin/bsondump: time stamp 2016-01-12 02:37:21 is 48736.074523763 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongofiles
tar: mongodb-linux-x86_64-3.2.1/bin/mongofiles: time stamp 2016-01-12 02:37:24 is 48739.014639938 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongooplog
tar: mongodb-linux-x86_64-3.2.1/bin/mongooplog: time stamp 2016-01-12 02:37:33 is 48747.960322499 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongoperf
tar: mongodb-linux-x86_64-3.2.1/bin/mongoperf: time stamp 2016-01-12 02:49:50 is 49484.63946251 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongod
tar: mongodb-linux-x86_64-3.2.1/bin/mongod: time stamp 2016-01-12 02:49:50 is 49484.277970529 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongos
tar: mongodb-linux-x86_64-3.2.1/bin/mongos: time stamp 2016-01-12 02:49:49 is 49483.120921568 s in the future
mongodb-linux-x86_64-3.2.1/bin/mongo
tar: mongodb-linux-x86_64-3.2.1/bin/mongo: time stamp 2016-01-12 02:49:50 is 49483.896681132 s in the future

Step 4: 复制解压文件到运行目录


mongo@mysql ~]$ pwd
/home/mongo
[mongo@mysql ~]$ mv mongodb-linux-x86_64-3.2.1 mongo
[mongo@mysql ~]$ ll
total 55108
drwxrwxr-x. 3 mongo mongo     4096 Jan 11 13:05 mongo
-rw-r-----. 1 mongo mongo 56419937 Jan 21  2016 mongodb-linux-x86_64-3.2.1.tgz

[mongo@mysql ~]$ 

Step 5: 设置环境变量


[mongo@mysql ~]$ vi .bash_profile 
PATH=$PATH:$HOME/bin:/home/mongo/mongo/bin
export PATH

Step 6: 创建数据目录


mkdir -p /data/mongodata

二、启动及验证mongodb

启动mongo

mongod --dbpath /home/mongo/mongo/data

以下内容为启动后输出的相关信息

[mongo@mysql ~]$ mongod -dbpath /home/mongo/mongo/data/
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] MongoDB starting :
pid=12579 port=27017 dbpath=/home/mongo/mongo/data/ 64-bit host=mysql
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] db version v3.2.1
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b2
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] modules: none
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] build environment:
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten]     distarch: x86_64
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/home/mongo/mongo/data/" } }
2016-01-11T13:08:33.725+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-01-11T13:08:33.828+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/home/mongo/mongo/data/diagnostic.data'
2016-01-11T13:08:33.829+0800 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-01-11T13:08:33.844+0800 I NETWORK  [initandlisten] waiting for connections on port 27017

停止mongo,直接使用ctrl + c

f^C2016-01-11T13:09:46.734+0800 I CONTROL[signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2016-01-11T13:09:46.734+0800 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2016-01-11T13:09:46.735+0800 I CONTROL  [signalProcessingThread] now exiting
2016-01-11T13:09:46.735+0800 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2016-01-11T13:09:46.735+0800 I NETWORK  [signalProcessingThread] closing listening socket: 5
2016-01-11T13:09:46.735+0800 I NETWORK  [signalProcessingThread] closing listening socket: 6
2016-01-11T13:09:46.735+0800 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2016-01-11T13:09:46.737+0800 I NETWORK[signalProcessingThread] shutdown: going to flush diaglog...
2016-01-11T13:09:46.737+0800 I NETWORK  [signalProcessingThread] shutdown: going to close sockets...
2016-01-11T13:09:46.737+0800 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2016-01-11T13:09:46.781+0800 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2016-01-11T13:09:46.781+0800 I CONTROL  [signalProcessingThread] dbexit:  rc: 0

修复启动过程中的两个警告,关于使用root用户启动mongo的警告先忽略

echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag

再次重启,后置于后台进程

mongod --dbpath /home/mongo/mongo/data &

查看启动后的进程

ps -ef|grep mongo

使用mongo连接到mongod

mongo
MongoDB shell version: 3.2.1


> help
    db.help()                    help on db methods
    db.mycoll.help()            help on collection methods
    sh.help()                    sharding helpers
    rs.help()                    replica set helpers
    help admin                  administrative help
    help connect                connecting to a db help
    help keys                    key shortcuts
    help misc                    misc things to know
    help mr                      mapreduce


    show dbs                    show database names
    show collections            show collections in current database
    show users                  show users in current database
    show profile                show most recent system.profile entries with time >= 1ms
    show logs                    show the accessible logger names
    show log [name]              prints out the last segment of log in memory, 'global' is default
    use                 set current database
    db.foo.find()                list objects in collection foo
    db.foo.find( { a : 1 } )    list objects in foo where a == 1
    it                          result of the last line evaluated; use to further iterate
    DBQuery.shellBatchSize = x  set default number of items to display on shell
    exit                        quit the mongo shell
    db.getCollection("version");
    test.version
    > exit
    bye

三、mongodb相关工具

在安装文件下有README,描述了常用的mongodb相关命令行工具

more /usr/local/mongodb/README
MongoDB README
Welcome to MongoDB!
COMPONENTS

bin/mongod - The database process.
bin/mongos - Sharding controller.
bin/mongo  - The database shell (uses interactive javascript).

UTILITIES

bin/mongodump        - MongoDB dump tool - for backups, snapshots, etc..
bin/mongorestore      - MongoDB restore a dump
bin/mongoexport      - Export a single collection to test (JSON, CSV)
bin/mongoimport      - Import from JSON or CSV
bin/mongofiles        - Utility for putting and getting files from MongoDB GridFS
bin/mongostat        - Show performance statistics

RUNNING

For command line options invoke:

$ ./mongod --help

To run a single server database:

$ mkdir /data/db
$ ./mongod
$
$ # The mongo javascript shell connects to localhost and test database by default:
$ ./mongo
> help

DRIVERS

 Client drivers for most programming languages are available at mongodb.org.  Use the
 shell ("mongo") for administrative tasks.

获取单个命令用法
–help

mongod –help|more

Options:

General options:
-h [ –help ] show this usage information
–version show version information

mongod –version

db version v3.2.1
git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2

Contents
  1. 1. 一、安装配置mongodb
  2. 2. Step 1: 设置系统环境及确保缺省端口27107可用
  3. 3. Step 2: 下载安装文件
  4. 4. Step 3: 解压下载的文件
  5. 5. Step 4: 复制解压文件到运行目录
  6. 6. Step 5: 设置环境变量
  7. 7. Step 6: 创建数据目录
  8. 8. 二、启动及验证mongodb
  9. 9. 三、mongodb相关工具
    1. 9.0.0.1. mongod –help|more
    2. 9.0.0.2. mongod –version